# -- coding: utf-8 --
# @Time : 2024/1/19 11:30
# @Author : RLH
# @File : Base_Tools_modlue.py
# 功能描述: 基础工具模块

import os
import os
import configparser


# 获取项目的根路径
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

class Base_Tools_Cls:

    def BasePathTrans_func(self, path):
        '''
        根据不同的操作系统，将路径进行转换为不同操作系统的路径
        :param path:
        :return:
        '''
        # 获取操作系统，Windows是nt; linx 是 posix
        os_type = os.name
        if os_type == 'nt':
            path = path.replace('/', '\\')
        elif os_type == 'posix':
            path = path.replace('\\', '/')
        else:
            path = path.replace('\\', '/')

        return os.path.join(root_dir, path)

    def file_dir_path(self,config_file,  *args):
        config = configparser.ConfigParser()
        config.read(config_file, encoding='utf-8')
        path = self.BasePathTrans_func(config.get(*args))
        return path

    def EnvironmentVariable_func(self, environment_path):
        # 获取当前PATH变量
        path_var = os.environ.get('PATH')

        # 将目录添加到PATH变量中（使用;作为分隔符，Windows系统）
        if path_var:
            os.environ['PATH'] = environment_path + os.pathsep + path_var
        else:
            os.environ['PATH'] = environment_path

    def read_config(self, config_file, section, option, trans_flag=True):
        config = configparser.ConfigParser()
        config_file = self.BasePathTrans_func(config_file)
        config.read(config_file, encoding='utf-8')
        if trans_flag:
            config_value = self.BasePathTrans_func(config.get(section, option))
        else:
            config_value = config.get(section, option)
        return config_value


BaseTools_Cls = Base_Tools_Cls()




